package com.zhilehuo.pretask.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.drawable.DrawableCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zhilehuo.pretask.databinding.ItemArticleBinding
import com.zhilehuo.pretask.ui.article.ArticleViewModel

/**
 * @Description: 分页响应结构
 * @Author: Realnt
 * @Date:
 */
data class ArticlePage(
    val total: Int,
    val list: List<Article>,
    val pageNum: Int,
    val pageSize: Int,
    val hasNextPage: Boolean
)

/**
 * @Description: 文章数据模型
 * @Author: Realnt
 * @Date:
 */
data class Article(
    val id: Int,
    val title: String,
    val wordNum: Int,
    val lexile: Int,
    val typeId: Int,
    val type: String,
    val cover: String,
    val stage: String?,
    val isRead: Int
)

/**
 * @Description: 文章封面适配器
 * @Author: Realnt
 * @Date:
 */
class ArticleAdapter : ListAdapter<Article, ArticleAdapter.ViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ViewHolder(ItemArticleBinding.inflate(LayoutInflater.from(parent.context), parent, false))

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding.apply {
            title.text = item.title
            val diffString = "难度:${item.lexile}"
            difficulty.text = diffString
            val wordNumString = "${item.wordNum}词"
            wordCount.text = wordNumString
            category.text = item.type

            /* 显示年龄 */
            item.stage?.takeIf { it.isNotEmpty() }?.let {
                stage.text = it
                stage.visibility = View.VISIBLE
            } ?: run {
                stage.visibility = View.GONE
            }

            /* 针对右下角标分类对应颜色 */
            val color = when (category.text) {
                "人工智能" -> Color.parseColor("#187B68")
                "前沿技术" -> Color.parseColor("#057150")
                "太空宇宙" -> Color.parseColor("#4F55ED")
                "生物医疗" -> Color.parseColor("#41B6F3")
                "自然科学" -> Color.parseColor("#71E84B")
                "环境生态" -> Color.parseColor("#FD9F34")
                "历史文化" -> Color.parseColor("#6F624F")
                "艺术文学" -> Color.parseColor("#C83576")
                "休闲生活" -> Color.parseColor("#3BA9A3")
                "社会现象" -> Color.parseColor("#AA60E6")
                "成长教育" -> Color.parseColor("#087432")
                "心理情感" -> Color.parseColor("#A725C8")
                "动物趣闻" -> Color.parseColor("#187B68")
                "传统文化" -> Color.parseColor("#187B68")
                else -> Color.GREEN
            }
            val backgroundDrawable = category.background
            DrawableCompat.setTint(backgroundDrawable, color)

            /* 加载封面图片 */
            Glide.with(root.context)
                .load(item.cover)
                .into(cover)
        }
    }

    class ViewHolder(val binding: ItemArticleBinding) : RecyclerView.ViewHolder(binding.root)

    /* DiffUtil计算差异增量更新 */
    object DiffCallback : DiffUtil.ItemCallback<Article>() {
        override fun areItemsTheSame(oldItem: Article, newItem: Article) = oldItem.id == newItem.id
        override fun areContentsTheSame(oldItem: Article, newItem: Article) = oldItem == newItem
    }
}